package com.hqyj.springbootdemo01.common.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.filter.CorsFilter;

/**
 * @author sdx2009
 * @package com.hqyj.springbootdemo01.common.config
 * @date 2022/8/25 15:34
 */
@Configuration
public class CorsWebConfig {

    @Bean
    public CorsFilter corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration config = new CorsConfiguration();
//        允许携带认证信息
        config.setAllowCredentials(true);
//        允许所有的请求头
        config.addAllowedHeader("*");
//        允许所有的请求源：比如：127.0.0.1
        config.addAllowedOriginPattern("*");
//        允许所有的请求方法
        config.addAllowedMethod("*");
        source.registerCorsConfiguration("/**", config);
        return new CorsFilter(source);
    }
}
